<template>
  <div class="upload-container">
    <el-upload
      name="images[]"
      :multiple="false"
      :show-file-list="false"
      :on-success="handleImageSuccess"
      :on-error="handleImageError"
      class="image-uploader"
      :headers="headers"
      drag
      :action="url+geturl+'/upload/images'"
    >
      <i v-if="!tempUrl" class="el-icon-plus avatar-uploader-icon" />
      <vimage class="tempimg" v-else :src="tempUrl"></vimage>
    </el-upload>

  </div>
</template>

<script>
// import { getToken } from '@/api/qiniu'
import { geturl } from "@/api/common";
import { getToken } from "@/utils/auth";
export default {
  name: "SingleImageUpload",
  props: {
    tempUrl: {
      type: String,
      default: ""
    }
  },
  data() {
    return {
      url: process.env.VUE_APP_BASE_API,
      headers: {},
      geturl: ""
      // tempUrl: '',
    };
  },
  computed: {
    imageUrl() {
      return this.value;
    }
  },
  created(){
    this.geturl = geturl();
  },
  mounted() {
    this.headers["Authorization"] = getToken();
  },
  methods: {
    rmImage() {
      this.emitInput("");
    },
    emitInput(val) {
      var data = {
        tempimg: val
      };
      this.$emit("input", data);
    },
    handleImageSuccess(a) {
      let tempUrl =a.msg[0];
      // tempUrl = tempUrl.replace("\\", "/");
      this.emitInput(tempUrl);
    },
    handleImageError() {},
    beforeUpload() {}
  }
};
</script>

<style lang="scss" scoped>
@import "~@/styles/mixin.scss";
.upload-container {
  width: 100%;
  height: 100%;
  position: relative;
  @include clearfix;
  .image-uploader {
    width: 100%;
    float: left;
    height: 100%;
  }
}
.tempimg {
  width: 100%;
  height: 100%;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
}
</style>
